using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Acms.WebControls
{
    public class MenuAdapter : System.Web.UI.WebControls.Adapters.MenuAdapter
    {
        protected override void RenderBeginTag(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Id, Control.ClientID);
            if (!string.IsNullOrEmpty(Control.CssClass))
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Class, Control.CssClass);
            }
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
        }

        protected override void RenderEndTag(HtmlTextWriter writer)
        {
            writer.RenderEndTag();
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            RenderItems(Control.Items, writer);
        }

        private void RenderItems(MenuItemCollection items, HtmlTextWriter writer)
        {
            if (items != null && items.Count > 0)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Class, string.Concat("list", items[0].Parent == null ? 0 : items[0].Parent.Depth + 1));
                writer.RenderBeginTag(HtmlTextWriterTag.Ul);
                foreach (MenuItem item in items)
                {
                    RenderItem(item, writer);
                }
                writer.RenderEndTag();
            }
        }

        private void RenderItem(MenuItem item, HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Class, string.Concat("level", item.Depth));
            writer.RenderBeginTag(HtmlTextWriterTag.Li);
            if (IsLink(item))
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Href, Page.ResolveUrl(item.NavigateUrl));
                writer.RenderBeginTag(HtmlTextWriterTag.A);
                writer.Write(item.Text);
                writer.RenderEndTag();
            }
            else
            {
                writer.RenderBeginTag(HtmlTextWriterTag.Span);
                writer.Write(item.Text);
                writer.RenderEndTag();
            }
            RenderItems(item.ChildItems, writer);
            writer.RenderEndTag();
        }

        private bool IsLink(MenuItem item)
        {
            return (item != null) && item.Enabled && !String.IsNullOrEmpty(item.NavigateUrl);
        }

        private string GetItemClass(MenuItem item)
        {
            string value = string.Concat("item", item.Depth);
            if (item != null)
            {
                value = GetSelectedClass(item);
            }
            return value;
        }

        private string GetSelectedClass(MenuItem item)
        {
            if (item.Selected)
            {
                return " selected";
            }
            else if (IsChildItemSelected(item))
            {
                return " childSelected";
            }
            else if (item.Parent != null && item.Parent.Selected)
            {
                return " parentSelected";
            }
            else
            {
                return "";
            }

        }

        private bool IsChildItemSelected(MenuItem item)
        {
            bool bRet = false;

            if ((item != null) && (item.ChildItems != null))
            {
                foreach (MenuItem child in item.ChildItems)
                {
                    if (child.Selected)
                    {
                        bRet = true;
                        break;
                    }
                    else
                    {
                        bRet = IsChildItemSelected(child);
                    }
                }
            }

            return bRet;
        }
    }
}
